package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.self.OwnPathEntity;
import com.swgk.core.customview.WSTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/6/21
 */
public class OwnPathsAdapter extends BaseRecycleAdapter<OwnPathEntity> {

    private Context context;
    private OwnPathItemClickListener clickListener;

    public OwnPathItemClickListener getClickListener() {
        return clickListener;
    }

    public void setClickListener(OwnPathItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public OwnPathsAdapter(Context context, List<OwnPathEntity> data) {
        super(context, R.layout.own_path_item_layout, data);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, OwnPathEntity pathEntity) {
        WSTextView loadAddress = holder.getView(R.id.load_and_unload);
        TextView pathType = holder.getView(R.id.types);
        TextView editBtn = holder.getView(R.id.edit_btn);
        TextView deleteBtn = holder.getView(R.id.delete_btn);
        TextView use_statue = holder.getView(R.id.use_statue);

        if (pathEntity.getStatus().equals("1") ) {
            //启用
            use_statue.setText("已启用");
            use_statue.setTextColor(context.getResources().getColor(R.color.color_06C764));
        } else {
            //禁用
            use_statue.setText("已禁用");
            use_statue.setTextColor(context.getResources().getColor(R.color.color_c0c4cc));
        }

        StringBuilder addressBl = new StringBuilder();
        if (pathEntity.getCity() != null && pathEntity.getCity().size() > 0) {
            for (CarriageSelectEntity entity : pathEntity.getCity()) {
                if (!TextUtils.isEmpty(entity.getName())) {
                    addressBl.append(entity.getName() + " ");
                }
            }
        }
        addressBl.append("→ ");
        if (pathEntity.getDestCity() != null && pathEntity.getDestCity().size() > 0) {
            for (CarriageSelectEntity entity : pathEntity.getDestCity()) {
                if (!TextUtils.isEmpty(entity.getName())) {
                    addressBl.append(entity.getName() + " ");
                }
            }
        }
        loadAddress.setText(addressBl.toString());

        String carTypeStr = "";
        if (pathEntity.getCarType() != null && pathEntity.getCarType().size() > 0) {
            for (CarriageSelectEntity entity : pathEntity.getCarType()) {
                if (!TextUtils.isEmpty(entity.getName())) {
                    carTypeStr = carTypeStr + entity.getName() + "/";
                }
            }
            if (carTypeStr.length() > 0) {
                carTypeStr = carTypeStr.substring(0, carTypeStr.length() - 1);
            }
        }

        String goodsTypeStr = "";
        if (pathEntity.getGoodsType() != null && pathEntity.getGoodsType().size() > 0) {
            for (CarriageSelectEntity entity : pathEntity.getGoodsType()) {
                if (!TextUtils.isEmpty(entity.getName())) {
                    goodsTypeStr = goodsTypeStr + entity.getName() + "/";
                }
            }
            if (goodsTypeStr.length() > 0) {
                goodsTypeStr = goodsTypeStr.substring(0, goodsTypeStr.length() - 1);
            }
        }
        pathType.setText((TextUtils.isEmpty(goodsTypeStr) ? "" : goodsTypeStr+ "  ")+(TextUtils.isEmpty(carTypeStr) ? "" : carTypeStr));

        editBtn.setOnClickListener(v -> {
            if (clickListener != null)
                clickListener.onEdit(pathEntity);
        });
        deleteBtn.setOnClickListener(v -> {
            if (clickListener != null)
                clickListener.onDelete(pathEntity);
        });
    }

    public interface OwnPathItemClickListener {
        void onEdit(OwnPathEntity entity);

        void onDelete(OwnPathEntity entity);
    }
}
